window.K=window.K||{"URL":{"defaultTemplates":""}};
window._jstemplates=window._jstemplates||{};
K=$.extend(K,{
	"Profile":{
		"View":null,
		"template":{"name":"default","url":"template.html"},
		"installation":function(){
			K.Profile.View=Backbone.View.extend({
			    "tagName":"div",
			    "className":"user-profile",
			    "template":_.template($("#_template>#t_profile").text()),
			    "data":false,
				"dataURL":false,
				"wrapper":null,
				"events":{"onclick":function(){}},
			    "render":function(json){  
			        this.$el.html(this.template(this.data||json));
			        this.$el.css("text-align","center");
			        return this.$el;
			     },"setData":function(data_url){
			    	if(typeof(data_url)=="string")this.dataURL=data_url;
			    	else{this.data=data_url;}
			    	return this;
			     },"setURL":function(url){
				    	this.dataURL=url;
				    	return this;
				 },"to":function($dom){
			    	 if(this.data){
			    		this.render(this.data);
			    		$dom.append(this.$el);
			    		this.activate();
			    	 }else{
			    		$.ajax({"url":this.dataURL||K.URL.profile,"dataType":"json","data":{"_":$.now()},
			    				"dom":$dom,"_this":this}).done(function(json){
							this._this.render(json);
							this.dom.append(this._this.$el);
							this._this.activate();
						}).fail(function(e1,e2,e3){
							K.Tool.log(e2);
						});
			    	}
			    	 return this;
			    },"activate":function(){
			    	var $menu=this.$el.find(".profile-menu"),pf=this;
			    	this.$el.find(".user-profile-name").on("click",function($menu){
			    		return function(event){
			    			event.stopPropagation();
			    			if(!$menu.is(":visible")){
			    				$menu.show();
			    			}
			    		};
			    	}($menu));
			    	$menu.on("click",function(event){
			    		event.stopPropagation();
			    	});
			    	$menu.find("li").each(function(){
			    		$(this).on("click",function($item,pf){
			    			return function(){
			    				pf.events.onclick($item);
			    			};
			    		}($(this),pf));
			    	});
			    	$("body").on("click",function($menu){
			    		return function(){
			    			$menu.hide();
			    		};
			    	}($menu));
			    },"click":function(fn){
			    	if(typeof(fn)=="function"){this.events.onclick=fn;}
			    	return this;
			    }
			});
		},"init":function($dom){
			$dom.empty();
			var pf=new K.Profile.View();
			pf.to($dom);
			return pf;
		}
	}
});
$.extend(_jstemplates,{"Profile":function(cb){
	if($("#_template").length<=0){
		$("body").append($("<div type='text/template' id='_template' />"));
	}
	var arr=[];
	arr.push('<script id="t_profile" type="text/template">');
	arr.push('{var hasMenu=false;');
	arr.push('if(typeof(menu)!="undefined"&&menu.length>0)hasMenu=true;}');
	arr.push('<div><img class="user-profile-icon" src="{-icon}"/></div>');
	arr.push('<a class="font-c2 user-profile-name" {if(!hasMenu){}style="cursor:default;"{}} title="{-name}">{-name}{if(hasMenu){}<span class="caret"></span>{}}</a>');
	arr.push('{if(hasMenu){}');
	arr.push('<ul class="profile-menu animated flipInY bg-c3" >');
	arr.push('{for(var i=0;i<menu.length;i++){var item=menu[i];}');
	arr.push('{if(typeof(item.type)=="string"){}');
	arr.push('{if($.trim(item.type.toLowerCase())=="separator"){}');
	arr.push('<li style="height:1px;border:none;border-top:1px solid #cecece;"></li>');
	arr.push('{continue;}');
	arr.push('{}}');
	arr.push('{}else if(typeof(item)=="string"&&item.indexOf("--")>=0){}');
	arr.push('<li style="height:1px;border:none;border-top:1px solid #cecece;"></li>');
	arr.push('{continue;}');
	arr.push('{}}');
	arr.push('<li class="font-c2" data-id="{-item.id}"><div class="profile-icon {-item.icon}"></div><nobr>{-item.text}</nobr></li>');
	arr.push('{}}');
	arr.push('</ul>{}}');
	if($("#_template").find("#t_profile").length<=0)
	$("#_template").append(arr.join(""));
	if(typeof(cb)=="function")cb();

}});